import { _decorator, Component, Node, CircleCollider2D, Contact2DType, Sprite, IPhysics2DContact, v3, Vec3, UITransform, size } from 'cc';
import { GlobalConfig } from '../../Init/Config/GlobalConfig';
import { GlobalEnum } from '../../Init/Config/GlobalEnum';
import EventManager from '../../Init/Managers/EventManager';
import { EventTypes } from '../../Init/Managers/EventTypes';
import { AudioEnum } from '../../Init/SystemAudio/AudioEnum';
import { AudioSystem } from '../../Init/SystemAudio/AudioSystem';
import GlobalPool from '../../Init/Tools/GlobalPool';
import { BasicEnemy } from './BasicEnemy';
import { BasicProp } from './BasicProp';
import { Zombie2D } from './Zombie2D';
const { ccclass, property } = _decorator;

@ccclass('Mine2D')
export class Mine2D extends BasicProp {
    _isBoom = false;
    _isReadyBoom = false;
    _readyTime = 0;
    //配置属性
    _atk = GlobalConfig.Mine.atkHpRate;
    _atkRadius = GlobalConfig.Mine.atkRadius;
    _boomDelay = GlobalConfig.Mine.boomDelay;

    init(parent: Node, p: Vec3, r: Vec3) {
        this.node.parent = parent;
        this.node.setPosition(p);
        this.node.eulerAngles = r;
        let trans = this.node.getComponent(UITransform);
        let radius = GlobalConfig.Mine.atkRadius * 2;
        trans.setContentSize(size(radius, radius));
        //
        this._isBoom = false;
        this._isReadyBoom = false;
        this._readyTime = 0;
        //
        this.mappingProps = {
            type: GlobalEnum.PropsType.Mine,
            node: this.node,
            data: { isReadyBoom: false }
        }
        this.initCollider();
        //
        let sp = this.node.getComponent(Sprite);
        if (sp) {
            sp.enabled = GlobalConfig.isShow2dCollider;
        }
    }

    update(dt: any): void {
        if (this._isBoom) return;
        this.boomCountDown(dt);
    }


    boomCountDown(dt) {
        if (!this._isReadyBoom || this._isBoom) return;
        this._readyTime += dt;
        if (this._readyTime >= this._boomDelay) {
            this._isBoom = true;
            //爆炸
            this.onBoom();
        }
    }
    _tmpP = v3();
    enemyArr = [GlobalEnum.EnemyTypes.People];
    onBoom() {
        //音效
        AudioSystem.playEffect(AudioEnum.Boom);
        //爆炸效果
        this._tmpP.x = this.node.position.x * GlobalConfig.Scale2DTo3D;
        this._tmpP.z = -this.node.position.y * GlobalConfig.Scale2DTo3D;
        this._tmpP.y = 1;
        EventManager.emit(EventTypes.EffectEvents.show2DEffect, { t: GlobalEnum.Effect2DType.MineBoom, p: this._tmpP });

        //检测附近平民、僵尸
        let outEnemy: { [t: number]: BasicEnemy } = {};
        EventManager.emit(EventTypes.EnemyEvents.CheckEnemyByType,
            { p: this.node.position, r: this._atkRadius, types: this.enemyArr, out: outEnemy })
        for (const key in outEnemy) {
            const e = outEnemy[key];
            e.byAtk(false);
        }
        let outZombies: Zombie2D[] = [];
        EventManager.emit(EventTypes.EnemyEvents.CheckZombies, { wp: this.node.worldPosition, r: this._atkRadius, outZombies: outZombies });
        for (let i = 0; i < outZombies.length; i++) {
            outZombies[i].byAtk(this._atk, true);
        }
        //回收
        GlobalPool.put(this.node);
        this.reset();
        this.resetCollider();
    }

    //#region 碰撞
    collider: CircleCollider2D = null;
    initCollider() {
        this.collider = this.node.getComponent(CircleCollider2D);
        if (this.collider) {
            this.collider.radius = GlobalConfig.Mine.atkRadius;
            this.collider.apply();
            this.collider.once(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
        }
    }
    resetCollider() {
        if (this.collider) {
            this.collider.off(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
        }
    }

    onBeginContact(selfCollider: CircleCollider2D, otherCollider: CircleCollider2D, contact: IPhysics2DContact | null) {
        if (!this._isReadyBoom && !this._isBoom) {
            this._isReadyBoom = true;
            this.mappingProps.data.isReadyBoom = true;

            EventManager.emit(EventTypes.RoleEvents.ShowDialog, GlobalEnum.DialogTargetType.Mine);
        }
    }

    //#endregion
}

